/**
 * 作息controller
 */
'use strict';

var response = require('./response');
var config = require('../conf/config');
var db = require('../conf/db.js');
var errorCode = config.errorCode;


exports.queryEnabled = function(req, res){
    var user = req.session.user;
    var Timetable = db.model(user.tenant, 'Timetable');

    Timetable.findOne({isEnabled : true}).exec(function (err, timetable) {
        if (err) {
            console.error('异常信息为：%s', JSON.stringify(err));
            response.error(res, config.errorCode.databaseError);
        } else {
            response.data(res, timetable);
        }
    });
};

exports.queryAll = function(req, res){
    var Timetable = db.model(req.session.user.tenant, 'Timetable');

    Timetable.find({}).exec(function (err, timetables) {
        if (err) {
            console.error('异常信息为：%s', JSON.stringify(err));
            response.error(res, config.errorCode.databaseError);
        } else {
            response.data(res, timetables);
        }
    });
};

exports.update = function(req, res){
    var user = req.session.user;
    var Timetable = db.model(user.tenant, 'Timetable');
    var id = req.body.id;
    var am = req.body.am;
    var pm = req.body.pm;

    Timetable.update({_id : id}, {$set : {am : am, pm : pm}}, function(err, count){
        if (err) {
            console.error('异常信息为：%s', JSON.stringify(err));
            response.error(res, config.errorCode.databaseError);
        } else {
            response.success(res);
        }
    });
};

exports.updateStatus = function(req, res){
    var user = req.session.user;
    var Timetable = db.model(user.tenant, 'Timetable');
    var id = req.params.id;
    var isEnabled = req.params.isEnabled;

    timetable.count({isEnabled : true}, function(err, count){
        if (err) {
            console.error('异常信息为：%s', JSON.stringify(err));
            response.error(res, config.errorCode.databaseError);
        } else {
            if (count > 1) {
                response.error(res, config.errorCode.onlyOneError);
            } else {
                Timetable.update({_id : id}, {$set : {isEnabled : isEnabled}}, function(err, count){
                    if (err) {
                        console.error('异常信息为：%s', JSON.stringify(err));
                        response.error(res, config.errorCode.databaseError);
                    } else {
                        response.success(res);
                    }
                });
            }
        }
    });
};

exports.create = function(req, res){
    var user = req.session.user;
    var Timetable = db.model(user.tenant, 'Timetable');
    var timetable = new Timetable(req.body);
    timetable.createUser = user.id;
    
    // 判断是否只有一个有效作息时间
    Timetable.count({isEnabled : true}, function(err, count){
        if (err) {
            console.error('异常信息为：%s', JSON.stringify(err));
            response.error(res, config.errorCode.databaseError);
        } else {
            timetable.isEnabled = count < 1;
            timetable.save(function(err, timetable){
                if (err) {
                    console.error('异常信息为：%s', JSON.stringify(err));
                    response.error(res, config.errorCode.databaseError);
                } else {
                    response.data(res, timetable);
                }
            });
        }
    });
};


